package mines.eerock.sphero.bowling;

import mines.eerock.sphero.bowling.audiobutton.MediaButtonBlocker;
import mines.eerock.sphero.bowling.robot.AssistiveRobot;
import orbotix.robot.app.StartupActivity;
import orbotix.robot.base.RobotProvider;
import orbotix.robot.widgets.calibration.CalibrationView;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

public class StartActivity extends Activity {
	
	private int m_state = 0;
	private AssistiveRobot mRobot = null;
	
	// Startup Activity Request Code
	private final static int STARTUP_ACTIVITY = 0;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.start);
        Intent i = new Intent(this, StartupActivity.class);  
		startActivityForResult(i, STARTUP_ACTIVITY); 
    }   
    
	/**
	 * Ball is connected, start States
	 */
	protected void onActivityResult(int requestCode, int resultCode, Intent data){
		super.onActivityResult(requestCode, resultCode, data);
		if(requestCode == STARTUP_ACTIVITY && resultCode == RESULT_OK){
			//Get the connected Robot
			final String robot_id = data.getStringExtra(StartupActivity.EXTRA_ROBOT_ID);  // 1
			if(robot_id != null && !robot_id.equals("")){
				mRobot = new AssistiveRobot(robot_id);
				// Let Calibration View know which robot we are connected to
				CalibrationView calibrationView = (CalibrationView)findViewById(R.id.calibration_widget);
				calibrationView.setRobot(RobotProvider.getDefaultProvider().findRobot(robot_id)); 
			}
		}
		nextState();
	}
    
	/**
	 * Loops through the states of showing the tutorials on how to set up bowling
	 */
	private void nextState() {
		m_state++;
		
		// get Text View and Next Button
		TextView info = (TextView)findViewById(R.id.txt_info);
		
		// Place Shero in open area, remember location.
		if(m_state == 1) {
			info.setText(this.getString(R.string.StartState1));
		}
		// Press Two Fingers Down and Spin until Blue Tail Light is Facing the Direction You Want to Bowl.
		else if(m_state == 2) {
			info.setText(this.getString(R.string.StartState2));
		}
		// Sphero will now roll to the Spot You Should Set Up The First Pin.
		else if(m_state == 3) {
			mRobot.driveStartUp();
			info.setText(this.getString(R.string.StartState3));
		}
		// Bring Sphero Back To Start Point
		else if(m_state == 4) {			
			info.setText(this.getString(R.string.StartState4));
		}
		// Lets Bowl! Press Physical Button To Start.
		else if(m_state == 5) {
			info.setText(this.getString(R.string.StartState5));
			MediaButtonBlocker.setBlocking(this, true);	
			// Start Bowling!
			startBowling();
		} 
		else if(m_state == 6) {
			// Start Bowling!
			Button quitBtn = (Button)findViewById(R.id.btn_start);
			quitBtn.setText(R.string.Quit);
			info.setText(this.getString(R.string.StartState6));
		}
		else {
			Intent intent = new Intent(this.getApplicationContext(), AssistiveSpheroBowlingActivity.class);
		    this.startActivity(intent);
			finish();
		}
	}
	
	/**
	 * Move to next state of the tutorial
	 * @param v
	 */
	public void onNextStep(View v) {
		nextState();
	}
	
	/**
	 * Only be able to calibrate in state 2
	 */
	public boolean dispatchTouchEvent(MotionEvent event) {
		 CalibrationView calibrationView = (CalibrationView)findViewById(R.id.calibration_widget);
		 
		 // State is in calibration mode
		 if( m_state == 2 ) {
				 calibrationView.interpretMotionEvent(event);
		 }
		 return super.dispatchTouchEvent(event);
	 }
	
	
	/**
	 * Starts power meter
	 */
	public void startBowling() {
		mRobot.startBowling(this);
		//loopPowerMeter();
	}	
	
	private void cleanUp() {
    	// Stop blocking false AudioJack Intents 
    	MediaButtonBlocker.setBlocking(this, false);
    	mRobot.cleanUp();
	}
	
    @Override
    protected void onPause()
    {
      //cleanUp();
      super.onPause();
    }
    
    @Override
    protected void onResume() {
    	super.onResume();
    }

    @Override
    protected void onDestroy()
    {
      cleanUp();
      super.onDestroy();
    }
    
	/**
	 * Finishes the activity.
	 */
	@Override
	public void onBackPressed() {
		cleanUp();
		finish();
	}	
}

